package com.yuzhihao.writejvm.attributes.stack;

import com.yuzhihao.writejvm.clazz.ClassInfo;
import com.yuzhihao.writejvm.format.PrintFormat;
import com.yuzhihao.writejvm.format.StackMapFrameEnum;
import com.yuzhihao.writejvm.read.Parse;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.log4j.Log4j2;

import java.io.DataInputStream;
import java.io.IOException;

/**
 * 251
 * @see  <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.4">jvmdoc</a>
 * @author yzh
 * @since 2021年12月14日13:32:39
 */
@Data
@Log4j2
@EqualsAndHashCode(callSuper = true)
public class SameFrameExtended extends StackMapFrame{

    private int offset_delta_u2 = 2;

    private int offset_delta;

    public SameFrameExtended() {
        super();
        super.setSmfe(StackMapFrameEnum.same_frame_extended);
    }

    @Override
    public void parse(DataInputStream dis, ClassInfo ci) {
        super.setCi(ci);
        try {
            this.offset_delta = Parse.byteToInt(this.offset_delta_u2, dis);
        } catch (IOException e) {
            log.error(e);
        }
    }

    @Override
    public String print(String print) {
        StringBuilder builder = new StringBuilder();
        builder.append(print)
                .append(PrintFormat.EMPTY)
                .append("frame_type")
                .append(PrintFormat.EQUAL)
                .append(super.frame_type)
                .append(PrintFormat.EMPTY)
                .append("/* same_frame_extended */")
                .append(System.lineSeparator());

        String format = print + PrintFormat.EMPTY +PrintFormat.EMPTY;
        builder.append(format)
                .append("offset_delta")
                .append(PrintFormat.EQUAL)
                .append(this.offset_delta)
                .append(System.lineSeparator());
        return builder.toString();
    }
}
